//加载长连接
//长连接推送ajax
var xmlHttp;
//发送消息ajax
var xmlHttpSender;
//发送消息主体
var sendMessage;
//发送消息优先级
var priority=1;
//长连接推送消息
var index=0;
var isEnd=false;
//接收消息地址
var url='http://192.168.130.115/ums-comet/poll';
//发送消息地址
var sendUrl='http://192.168.130.115/ums-comet/push';
//发送群消息地址
var sendGroupUrl='http://192.168.130.115/ums-comet/pushGroup';
//当前用户
var currentUser;

//用户1
var Tom={
	no:1,
	fromName:"Tom",
	fromUid:900000122,
	fromAvatarUrl:"111111",
	toUid:10000004219,
	toGroupId:100002
};
//用户2
var Jerry={
    no:2,
	fromName:"Jerry",
	fromUid:999999,
	fromAvatarUrl:"888888",
	toUid:100000,
	toGroupId:100002
};
var Marz={
	no:3,
	fromName:"Marz",
	fromUid:555555,
	fromAvatarUrl:"333333",
	toUid:100000,
	toGroupId:100002	
};
//公共方法
var commonMethed={
		//获取ajax对象
		getHttpObject:function(){
			var baseXmlHttp;
			if (typeof XMLHttpRequest != "undefined") {
				baseXmlHttp = new XMLHttpRequest();
			} else if (window.ActiveXObject) {
				var aVersions = [ "Msxml2.XMLHttp.5.0", "Msxml2.XMLHttp.4.0",
						"Msxml2.XMLHttp.3.0", "Msxml2.XMLHttp", "Microsoft.XMLHttp" ];
				for ( var i = 0; i < aVersions.length; i++) {
					try {
						baseXmlHttp = new ActiveXObject(aVersions[i]);
						break;
					} catch (e) {
					}
				}
			}
			return baseXmlHttp;
		},
		//中断ajax请求
		stop: function(xmlHttp){
			if (xmlHttp) {
				xmlHttp.abort();
			}
		},
		//初始化用户角色
		initUser:function(value){
			if(value==1){
				currentUser=Tom;
			}
			if(value==2){
				currentUser=Jerry;
			}
			if(value==3){
				currentUser=Marz;
			}
			document.getElementById('loginAccount').value=currentUser.fromName;
			//开始接收消息
			receiver.start();
		},
		//初始化用户1ajax请求头
		initRequestTom:function(xmlHttp){
			xmlHttp.setRequestHeader("X-ums-DeviceId","qaz4xsw56edc123");
			xmlHttp.setRequestHeader("X-ums-Uid","100000");
			xmlHttp.setRequestHeader("X-ums-AppId","b68d9f306d2b48178f8df45084415d10");
			xmlHttp.setRequestHeader("X-ums-token","783b3924c6ac4f4f928");
			xmlHttp.setRequestHeader("X-ums-Timestamp","123456789");
			xmlHttp.setRequestHeader("X-ums-Sign","F72BBBD8A21D4F1C5B197A0ADE3F7371");
		},
		//初始化用户2ajax请求头
		initRequestJerry:function(xmlHttp){
			xmlHttp.setRequestHeader("X-ums-DeviceId","qaz4xsw56edc123");
			xmlHttp.setRequestHeader("X-ums-Uid","999999");
			xmlHttp.setRequestHeader("X-ums-AppId","b68d9f306d2b48178f8df45084415d10");
			xmlHttp.setRequestHeader("X-ums-token","d0af714b32f9422d90a34bf834521af2");
			xmlHttp.setRequestHeader("X-ums-Timestamp","123456789");
			xmlHttp.setRequestHeader("X-ums-Sign","426083265D08F2863958363DCC4340FC");
		},
		//初始化用户3ajax请求头
		initRequestMarz:function(xmlHttp){
			xmlHttp.setRequestHeader("X-ums-DeviceId","qaz4xsw56edc123");
			xmlHttp.setRequestHeader("X-ums-Uid","555555");
			xmlHttp.setRequestHeader("X-ums-AppId","b68d9f306d2b48178f8df45084415d10");
			xmlHttp.setRequestHeader("X-ums-token","2a7e3f56776d4a768e228a5dba5fb654");
			xmlHttp.setRequestHeader("X-ums-Timestamp","123456789");
			xmlHttp.setRequestHeader("X-ums-Sign","426083265D08F2863958363DCC4340FC");
		},
		//选择初始化请求头
		initRequest:function(xmlHttp){
			var status=currentUser.no;
			if(status==1){
				commonMethed.initRequestTom(xmlHttp);
			}else if(status==2){
				commonMethed.initRequestJerry(xmlHttp);
			}else{
				commonMethed.initRequestMarz(xmlHttp);
			}

		},
		//切换发送消息优先级
		switchPriority:function(){
			if(priority==1){
				priority=2;
			}else{
				priority=1;
			}
			document.getElementById('priority').innerHTML="当前消息优先级为"+(priority==1?"低":"中");
		}
		
}
//接受消息
var receiver={
	//ajax改变状态操作
	readyStateChange:function (){
		if (xmlHttp.readyState == 3 && xmlHttp.status == 200) {
			if (typeof index == "undefined"){
				index = 0;
			}
			var content = xmlHttp.responseText,
				msgText = content.substring(index),
				a = "|------|";
			index = content.length;
			console.log(msgText);
			if(msgText==''||a == msgText){
				return;
			}
			var msgSplit = msgText.split(a),
					msgtrim = msgSplit[0].trim();
			try {
				var result  = eval('('+msgtrim+')');
				receiver.addRecord(result);
			} catch (e) {
				//异常处理
				alert('连接中断');
				isEnd = true;
			}
		} else if (xmlHttp.readyState == 4) {
			if(!isEnd){
				index=0;
				receiver.request();
			}else{
				isEnd=false;
			}
		}
	},
	//发起ajax请求
	request:function(){
		if (xmlHttp) {
			if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
				xmlHttp.open("POST", url, true);
				xmlHttp.onreadystatechange = function(){
					receiver.readyStateChange();
				};
				commonMethed.initRequest(xmlHttp);
				xmlHttp.send();
			}
		}
	},
	//页面添加聊天记录
	addRecord:function(message){
		var date=message.createTime,
			fromUserAccount=message.fromNick;
		date=new Date(date);
		var p = document.createElement('p');
		var msg = fromUserAccount + ' - ' + date + message.msg;
		p.innerHTML = msg;
		if(message.toGroupId!="undefined"&&message.toGroupId!=null){
			document.getElementById('chatRecordsOther').appendChild(p);
		}else{
			document.getElementById('chatRecords').appendChild(p);
		}
	},
	//业务调用
	start:function(URL){
		xmlHttp=commonMethed.getHttpObject();
		if(URL!=''&&URL!=undefined){
			url=URL;
		}
		receiver.request();
	}
	
}
//发送消息
var sender={
	isSingle:true,
	url:sendUrl,
	sendMessage: function(){
		var msg=sender.buildMessage(),
		getUrl=sender.url+msg;
		xmlHttpSender.open("GET", getUrl, true);
		commonMethed.initRequest(xmlHttpSender);
		xmlHttpSender.onreadystatechange=function(){
			sender.readyStateChange();
		};
		xmlHttpSender.send();		
	},
	readyStateChange:function(){
		if (xmlHttpSender.readyState == 4) {
           if (xmlHttpSender.status == 200) {//200代表执行成功
//        	   sender.addRecord();
	       }
		}
	},
	//页面添加聊天记录
	addRecord:function(){
		var date=sendMessage.createTime,
			fromUserAccount=sendMessage.fromName;
		date=new Date(date);
		var p = document.createElement('p');
		var msg = fromUserAccount + ' - ' + date + sendMessage.msg;
		p.innerHTML = msg;
		document.getElementById('chatRecords').appendChild(p);
	},
	buildMessage:function(){
		var msg=document.getElementById('msg').value;
//		sendMessage={
//			'createTime':new Date,
//			'fromAvatarUrl':currentUser.fromAvatarUrl,
//			'fromName':currentUser.fromName,
//			'fromUid':currentUser.fromUid,
//			'toUid':currentUser.toUid,
//			'msg':msg,
//			'priority':priority,
//			'toGroupId':sender.
//			'msgType':1
//		}
		var String;
		if(sender.isSingle){
			String = "?createTime="+new Date+"&fromAvatarUrl="+currentUser.fromAvatarUrl+"&fromName="+currentUser.fromName
					+"&fromUid="+currentUser.fromUid+"&toUid="+currentUser.toUid+"&msg="+msg+"&msgType="+1+"&priority="+priority;
		}else{
			msg=document.getElementById('msgOther').value;
			String = "?createTime="+new Date+"&fromAvatarUrl="+currentUser.fromAvatarUrl+"&fromName="+currentUser.fromName
					+"&fromUid="+currentUser.fromUid+"&toGroupId="+currentUser.toGroupId+"&msg="+msg+"&msgType="+1+"&priority="+priority;
		}
		return String;
	},
	start:function(flag){
		if(currentUser==null||currentUser==undefined){
			alert("请选择用户角色");
			return;
		}
		if(flag){
			sender.isSingle=false;
			sender.url=sendGroupUrl;
		}
		xmlHttpSender=commonMethed.getHttpObject();
		sender.sendMessage();
	}
}
